<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增轮播图')"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-picture-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">图片：</label>
            <div class="col-sm-8 fileinput fileinput-new" data-provides="fileinput"
                 style="text-align: center; margin-bottom: 0;">
                <div class="fileinput-preview thumbnail" data-trigger="fileinput"
                     style="width: 200px; height: 150px; margin-right: 10px;"></div>
                <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span>
                    <span class="fileinput-exists">更改</span><input name="photo" type="hidden" value="">
                    <input id="williamYe" type="file"></span>
                <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">轮播图名称：</label>
            <div class="col-sm-8">
                <input name="photoName" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">轮播图链接：</label>
            <div class="col-sm-8">
                <input name="photoLink" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">排序：</label>
            <div class="col-sm-8">
                <input name="orderNum" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input name="remark" class="form-control" type="text">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script type="text/javascript">
    var prefix = ctx + "control/picture"

    $("#form-picture-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            save(prefix + "/add", $('#form-picture-add').serialize(), function (res) {
                if (res.success) {
                    $.modal.close();
                    parent.closeNowTab();
                    window.parent.$.modal.openTab("轮播图", $.common.setURL("/control/picture"));
                }
            });
        }
    }

    function save(url, data, callback) {
        var config = {
            url: url,
            type: "post",
            dataType: "json",
            data: data,
            beforeSend: function (request) {
                request.setRequestHeader("X-Access-Token", storage.get("token"));
            },
            success: function (result) {
                callback(result);
            }
        };
        $.ajax(config)
    }

    $('#williamYe').on('change.bs.fileinput ', function (e) {
        // 处理自己的业务
        if (this.files[0]) {
            const formData = new FormData();
            formData.append('file', this.files[0]);
            $.operate.fileUpload(formData, $('input[name="photo"]'));
        } else {
            $('input[name="photo"]').val("");
        }
    });
</script>
</body>
</html>
